﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>采购到货明细</title>
    <link href="//unpkg.com/layui@2.10.3/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <form class="layui-form" action="" id="baseStoreForm">
        <div class="layui-input-inline">

            <label class="layui-input-inline" style="width:auto">单据编码</label>
            <div class="layui-input-inline">
                <input type="text" name="documentCode" class="layui-input">
            </div>

            <label for="purchaseDepartment">单据状态:</label>
            <div class="layui-input-inline">
                <select id="purchasingid" name="purchasingid">
                    <option value="">请选择</option>
                    <option value="开立">开立</option>
                    <option value="审核">审核</option>
                    <option value="退回">退回</option>
                </select>
            </div>

            <label for="purchaseDepartment">业务状态:</label>
            <div class="layui-input-inline">
                <select id="businessStatus" name="businessStatus">
                    <option value="">请选择</option>
                    <option value="正常">正常</option>
                    <option value="挂起">挂起</option>
                    <option value="手工关闭">手工关闭</option>
                    <option value="系统关闭">系统关闭</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <div class="layui-inline">
                    <label class="layui-input-inline" style="width:auto">日期范围</label>
                    <div class="layui-inline" id="ID-laydate-range">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="modeDateStart" class="layui-input" id="begindate" placeholder="开始日期">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="modeDateEnd" class="layui-input" id="enddate" placeholder="结束日期">
                        </div>
                    </div>
                </div>
            </div>
            <button class="layui-btn" lay-submit lay-filter="search">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
    <table class="layui-hide" id="ID-table-demo-filter"></table>
    <script src="//unpkg.com/layui@2.10.3/dist/layui.js"></script>

    <!-- 操作列按钮模板 -->
    <script type="text/html" id="tableToolbar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    </script>

    <script>
        layui.use(['form', 'table', 'laydate', 'layer'], function () {
            var $ = layui.$;
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;
            var layer = layui.layer;

            // 初始化日期选择器
            laydate.render({elem: '#begindate', type: 'date'});
            laydate.render({elem: '#enddate', type: 'date'});

            // 渲染表格
            table.render({
                elem: '#ID-table-demo-filter',
                toolbar: 'default',
                height: 315,
                cols: [[
                    {field: 'purchaseArrivaId', title: '序号', width: 80, fixed: 'left'},
                    {field: 'documentCode', title: '单据编码', width: 120},
                    {field: 'documentStatus', title: '单据日期', width: 120},
                    {field: 'bocumentStatus', title: '单据状态', width: 100},
                    {
                        field: 'documentid',
                        title: '业务类型',
                        width: 100,
                        templet: function(d){
                            const typeMap = {'1': '零部件', '2': '原材料', '3': '五金'};
                            return typeMap[d.documentid] || d.documentid;
                        }
                    },
                    {field: 'businessStatus', title: '业务状态', width: 100},
                    {field: 'purchasingTiem', title: '审核日期', width: 120},
                    {
                        title: '操作',
                        width: 120,
                        fixed: 'right',
                        toolbar: '#tableToolbar'
                    }
                ]],
                done: function () {
                    // 记录筛选状态
                    var that = this;
                    that.elem.next().on('mousedown', 'input[lay-filter="LAY_TABLE_TOOL_COLS"]+', function () {
                        var input = $(this).prev()[0];
                        layui.data('table-filter-test', {
                            key: input.name,
                            value: input.checked
                        });
                    });
                }
            });

        table.on('tool(ID-table-demo-filter)', function (obj) {
            if (obj.event === 'edit') {
                var data = obj.data;
                // 将整行数据编码为JSON并传递
        // @ViewBag.BaseUrl/T7/Procurements/PutPurchaseArrival?data=${encodedData}
                var encodedData = encodeURIComponent(JSON.stringify(data));
                    var targetUrl = `/Procurements/PutPurchaseArrival?data=${encodedData}`;

                try {
                    window.location.href = targetUrl;
                } catch (error) {
                    console.error('跳转失败:', error);
                    layer.msg('跳转失败，请检查网络或联系管理员', { icon: 2 });
                }
            }
        });

            function loadRoleData() {
                layer.load(2);
                var params = {
                    documentCode: $('[name="documentCode"]').val(),
                    businessStatus: $('[name="businessStatus"]').val(),
                    modeDateStart: $('[name="modeDateStart"]').val(),
                    modeDateEnd: $('[name="modeDateEnd"]').val(),
                };

                $.ajax({
                    //url: 'http://localhost:5234/api/PurchaseInvoice/DetailsPurchaseArrival',
                     url: "@ViewBag.BaseUrl/T7/PurchaseInvoice/DetailsPurchaseArrival",
                    data: params,
                    type: 'get',
                    dataType: 'json',
                    success: function (res) {
                        layer.closeAll('loading');
                        if (res.code === 210) {
                            table.reload('ID-table-demo-filter', {
                                data: res.data,
                            });
                        } else {
                            layer.msg('获取数据失败: ' + (res.message || '未知错误'), {icon: 2});
                        }
                    },
                    error: function (xhr, status, error) {
                        layer.closeAll('loading');
                        layer.msg('请求失败: ' + error, {icon: 2});
                    }
                });
            }

            // 监听查询按钮提交事件
            form.on('submit(search)', function (data) {
                loadRoleData();
                return false;
            });

            // 初始加载数据
            loadRoleData();
        });
    </script>
</body>
</html>